package com.example.xymarket.httpUtil;

import java.net.URL;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.util.Log;
import android.widget.ImageView;

import com.example.xymarket.common.MyBitMap;

public class AsyncImageLoader {

    private Handler handler = new Handler();

    private final Integer MaxSize=23;

    private ExecutorService executorService = Executors.newFixedThreadPool(5);

    // 引入线程池来管理多线程
    public void loadImage(final String url, final ImageView imageView) {
        executorService.submit(new Runnable() {
            public void run() {
                try {
                    Log.i("AdapterActivity", "Thread ID:" + Thread.currentThread().getId());
                    final Drawable drawable = Drawable.createFromStream(new URL(url).openStream(), "image.png");

                    // 模拟网络延时
                    handler.post(new Runnable() {
                        public void run() {
                            Log.i("AdapterActivity", "UpdateUIThread ID:" + Thread.currentThread().getId());
                            Drawable newDrawable = MyBitMap.getNewDrawable(drawable, 500, 500);
                            imageView.setImageDrawable(newDrawable);
                        }
                    });
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        });
    }

    public int getMinKey(Map<Integer,Drawable> map)
    {
        Set<Integer> set = map.keySet();
        Object[] obj = set.toArray();
        Arrays.sort(obj);
        return Integer.parseInt(obj[obj.length - 1].toString());
    }

    public int getMaxKey(Map<Integer,Drawable> map)
    {
        Set<Integer> set = map.keySet();
        Object[] obj = set.toArray();
        Arrays.sort(obj);
        return Integer.parseInt(obj[0].toString());
    }


    public void loadImage(final String url, final ImageView imageView,final Map<Integer,Drawable> map,int position) {
        executorService.submit(new Runnable() {
            public void run() {
                try {
                    Log.i("AdapterActivity", "Thread ID:" + Thread.currentThread().getId());
                    final Drawable drawable = Drawable.createFromStream(new URL(url).openStream(), "image.png");

                    // 模拟网络延时
                    handler.post(new Runnable() {
                        public void run() {
                            Log.i("AdapterActivity", "UpdateUIThread ID:" + Thread.currentThread().getId());
                            Drawable newDrawable = MyBitMap.getNewDrawable(drawable, 500, 500);
                            if(map.size()< MaxSize)
                            {
                                map.put(position,newDrawable);
                            }
                            else
                            {
                                if(position>getMaxKey(map))
                                {
                                    map.remove(getMinKey(map));
                                    map.put(position,drawable);
                                }
                                if(position<getMinKey(map))
                                {
                                    map.remove(getMaxKey(map));
                                    map.put(position,drawable);
                                }
                            }
                            imageView.setImageDrawable(newDrawable);
                        }
                    });
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        });
    }

    public void loadImage2(final String url, final ImageView imageView, final Map<Integer,Drawable> map) {
        executorService.submit(new Runnable() {
            public void run() {
                try {
                    Log.i("AdapterActivity", "Thread ID:" + Thread.currentThread().getId());
                    final Drawable drawable = Drawable.createFromStream(new URL(url).openStream(), "image.png");

                    // 模拟网络延时
                    handler.post(new Runnable() {
                        public void run() {
                            Log.i("AdapterActivity", "UpdateUIThread ID:" + Thread.currentThread().getId());
                            Drawable newDrawable = MyBitMap.getNewDrawable(drawable, 500, 500);
                            imageView.setImageDrawable(newDrawable);
                            map.put(imageView.getId(),newDrawable);
                        }
                    });
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        });
    }
}
